using UnityEngine;
using System.Collections.Generic;
using System;
using System.Linq;
using Framework.Save;
using LitJson;

namespace GameCore
{
    public class IngredientGroup : MonoBehaviour, IJsonSerializable
    {
        public Action OnItemPlaced;
        
        public List<IngredientCell> ingredients;

        private void Start()
        {
            foreach (var ingredientCell in ingredients)
            {
                ingredientCell.OnItemPlaced = ItemPlaced;
            }
        }

        private void ItemPlaced() => OnItemPlaced?.Invoke();

        public bool Match() => ingredients.All(ingredient => ingredient.Match());
        
        public JsonData Serialize()
        {
            var json = JsonUtil.NewArray();
            for (int i = 0; i < ingredients.Count; i++)
            {
                json.Add(ingredients[i].Serialize());
            }
            return json;
        }

        public void Deserialize(JsonData json)
        {
            for (int i = 0; i < ingredients.Count; i++)
            {
                ingredients[i].Deserialize(json[i]);
            }
        }
    }
}